import 'dart:async';

import 'package:flutter/material.dart';
import 'package:laksa/screens/login_screen.dart';
import 'package:laksa/services/login_service.dart';
import 'package:laksa/widgets/laksa_dialog.dart';

class LoginScreenState extends State<LoginScreen> {

  String _userName;
  String _password;
  final TextEditingController _usernameController = new TextEditingController();
  final TextEditingController _passwordController = new TextEditingController();

  @override
  build(BuildContext context) =>
      new Scaffold(
        body: new Stack(children: <Widget>[
          new Opacity(
              opacity: 1.0,
              child: new Container(
                decoration: new BoxDecoration(
                  image: new DecorationImage(
                    image: new ExactAssetImage('assets/images/login_bg.gif'),
                    fit: BoxFit.cover,
                  ),
                ),
              )
          ),
          new Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              new Center(
                  child: new Image.asset(
                    'assets/images/logo.jpg',
                    width: MediaQuery
                        .of(context)
                        .size
                        .width * 0.4,
                  )
              ),
              new Container(
                width: MediaQuery
                    .of(context)
                    .size
                    .width * 0.96,
                child: new Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    new TextField(
                      controller: _usernameController,
                      decoration: new InputDecoration(
                          labelText: '登录名',
                          errorText: (_userName == null) ? "" : _userName,
                          icon: new Icon(Icons.account_circle)
                      ),
                      onChanged: (String value) {
                        setState(() {
                          if (value.isEmpty) {
                            _userName = "登录用户不能为空";
                          } else if (value.length < 3) {
                            _userName = "登录用户少于3位";
                          } else {
                            _userName = "";
                          }
                        });
                      },
                    ),
                    new TextField(
                      controller: _passwordController,
                      obscureText: true,
                      decoration: new InputDecoration(
                          labelText: '密码',
                          errorText: (_password == null) ? "" : _password,
                          icon: new Icon(Icons.lock_outline)
                      ),
                      onChanged: (String value) {
                        setState(() {
                          if (value.isEmpty) {
                            _password = "密码不能为空";
                          } else if (value.length < 6) {
                            _password = "密码长度少于6位";
                          } else {
                            _password = "";
                          }
                        });
                      },
                    )

                  ],
                ),
              ),
              new Center(
                  child: new Column(
                    children: <Widget>[
                      new FlatButton(
                          onPressed: () {
                            _handleSubmitted();
                          },
                          child: new Container(
                              decoration: new BoxDecoration(
                                  color: Theme
                                      .of(context)
                                      .accentColor
                              ),
                              child: new Center(
                                child: new Text("登 录", style: new TextStyle(
                                  color: const Color(0xff000000),
                                )),
                              )
                          )
                      ),
                      new FlatButton(
                          onPressed: () {
                            print("foget password!");
                          },
                          child: new Text("忘记密码？", style: new TextStyle(
                            color: const Color(0xff000000),
                          ))
                      )
                    ],
                  )
              ),
              new Center(
                child: new FlatButton(
                    onPressed: () {
                      print("create new account!");
                    },
                    child: new Text("没有账号？创建一个吧", style: new TextStyle(
                      color: const Color(0xff000000),
                    ))
                ),
              )
            ],
          ),
        ]),
      );

  Future _handleSubmitted() async {
    if (_usernameController.text == '' || _passwordController.text == '') {
      await showMessage(context, "用户名或密码为空", DialogType.ERROR);
      return;
    } else if (_userName.isNotEmpty || _password.isNotEmpty) {
      await showMessage(context, "用户名或密码格式不正确", DialogType.ERROR);
      return;
    }
    LoginService.login(
        context, _usernameController.text, _passwordController.text);
  }
}
